package com.sunnada.edu.system.sysrole.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.dao.PubDao;

public class RoleMethods {
	public static String getCommonRole() {
		PubDao dao = AppContext.getInstance().getPubDao();
		List l = dao
				.selectList("select rec_id from s_sysrole where role_sign='"
						+ Constants.USER_COMMON_ROLE + "'");
		if (l != null && l.size() > 0) {
			return (String) ((Map) l.get(0)).get("REC_ID");
		}
		return null;
	}

	public static boolean isAdminRole(HttpServletRequest request) {
		String userId = (String) request.getSession().getAttribute(
				Constants.USER_ID_SESSION);
		if ("admin9527".equals(userId)) {
			return true;
		}
		return false;
	}

	/**
	 * 新增一个角色，默认都会赋给超级管理员账号：admin和自己
	 * @param params
	 * @return
	 */
	public static int grentRoleToAdmin(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String roleId = params.get("recId");
		String loginUserId = params.get("loginUserId");
		String sql="";
		int num=0;
		String recId =UUID.randomUUID().toString();
		if(!"admin".equals(loginUserId)){
			String recIds =UUID.randomUUID().toString();
			sql = "insert into s_sysroleuser(REC_ID,ROLE_ID,USER_ID,REC_PERSON,REC_TIME)"
					+ "values('"+recIds+"','"+roleId+"','admin','"+loginUserId+"',now())";
			num = dao.insert(sql);
			String rec =UUID.randomUUID().toString();
			sql = "insert into s_sysroleuser(REC_ID,ROLE_ID,USER_ID,REC_PERSON,REC_TIME)"
					+ "values('"+rec+"','"+roleId+"','agent','"+loginUserId+"',now())";
			num = dao.insert(sql);
		}
		sql = "insert into s_sysroleuser(REC_ID,ROLE_ID,USER_ID,REC_PERSON,REC_TIME)"
				+ "values('"+recId+"','"+roleId+"','"+loginUserId+"','"+loginUserId+"',now())";
		dao.insert(sql);
		return num;

	}
	
	
	public static List getRoleUsedList(HashMap<String, String> params){	
		PubDao dao = AppContext.getInstance().getPubDao();
		String recIds=params.get("recIds");
		String sql="  select * from S_SYSROLEUSER  where ROLE_ID in("+recIds+") ";
		return dao.selectList(sql);	
	}
	
	
	public static void deleteUserRole(HashMap<String, String> params){	
		PubDao dao = AppContext.getInstance().getPubDao();
		String recIds=params.get("recIds");
		String sql="  delete  from S_SYSROLEUSER  where ROLE_ID in("+recIds+") ";
		dao.delete(sql);	
	}
	
	
	public static void insertModuleToRole(HashMap<String, String> params){	
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId=UUID.randomUUID().toString();
		String moduleId=params.get("moduleId");
		String sql="insert into S_SYSMODULEROLE (REC_ID,ROLE_ID,MODULE_ID,REC_PERSON,REC_TIME) values('"+recId+"','1','"+moduleId+"','admin',now())";
		dao.insert(sql);	
	}
}
